package com.example.vehiclemanagesystem.controller;

import com.example.vehiclemanagesystem.entity.Driver;
import com.example.vehiclemanagesystem.entity.Notice;
import com.example.vehiclemanagesystem.entity.Vehicle;
import com.example.vehiclemanagesystem.service.DriverService;
import com.example.vehiclemanagesystem.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @PostMapping("/addNotice")
    public Map<String, Object> addNotice(@RequestBody Map<String, String> remap) {
        System.out.println("调用添加通知功能");
        boolean necessary=remap.containsKey("necessary");
        //boolean necessary=Boolean.getBoolean(remap.get("necessary"));
        String name=remap.get("name");
        String way=remap.get("way");
        String docx=remap.get("docx");
        String date1=remap.get("date1");
       // date1=date1.substring(0,17);
        String date2=remap.get("date2");
        String type=remap.get("type");
        Map<String, Object> map = new HashMap<>();
        try {
            Notice notice =new Notice(necessary,name,way,docx,date1,date2,type);
                noticeService.addNewNotice(notice);
                map.put("success", true);
                map.put("message", "通知添加成功");
                map.put("返回通知信息",notice);

        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("message", "添加失败！");
        }
        return map;
    }
    /*@RequestMapping("/allNotice")
    public Map<String,Object> printDriver(@RequestParam("query") String input){
        List<Notice> noticeList=noticeService.allNotice();
        Map<String, Object> map = new HashMap<>();
        map.put("整个通知表",noticeList);
        map.put("success",true);
        return map;
    }*/

    @RequestMapping("/allNotice")
    public Map<String, Object> thisNotice(@RequestParam("query") String input) {
        Map<String, Object> map = new HashMap<>();
        if (input == null || input.equals("")) {
            List<Notice> noticeList = noticeService.allNotice();
            map.put("pagenum", 1);
            int total = noticeList.size();
            map.put("total", total);
            map.put("success", true);
            map.put("整个通知表", noticeList);
            return map;
        }
        List<Notice> notice=noticeService.selectNotice(input);
        if (notice == null) {
            map.put("success", false);
            map.put("message", "消息不存在");
        } else {
            map.put("success", true);
            map.put("message",/*d_id+*/"搜索成功");
            map.put("整个通知表", notice);
        }
        return map;
    }
}
